#!/usr/bin/python
# -*- coding: UTF-8 -*-

from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
import time


class IPMCSET_UID_001(Case):
    """
            Name:
                IPMCSET_UID_001
            Label:
                设置UID指示灯状态
            PreCondition:
                BMC正常启动
            TestStep:
                1) 命令行执行 ipmcset -d identify [-v {time | force} ]，设置后查看命令ipmcget -d ledinfo预期结果1
            ExpectedResult:
                A) uid灯和设置的一致
            Parameters:

            Author:
                baixin liuya
            Last Modified:

            Remark:

        """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 查看LED 信息")
        resp = self.bmc_host.run({"command": ["ipmcget -d ledinfo"], "timeout": 10})

        self.log_step("Step2: 设置LED状态位force")
        self.bmc_host.run({"command": ["ipmcset -d identify -v force"], "timeout": 10})

        self.log_step("Step3: 查看LED 信息")
        resp = self.bmc_host.run({"command": ["ipmcget -d ledinfo"], "timeout": 10})
        if "ON" not in resp['stdout']:
            self.assertIsNotNone(None, "设置LED状态位force失败")

        self.log_step("Step4: 设置LED 100s")
        self.bmc_host.run({"command": ["ipmcset -d identify -v 100"], "timeout": 10})

        self.log_step("Step5: 等待90s，查看LED 信息")
        time.sleep(90)
        resp = self.bmc_host.run({"command": ["ipmcget -d ledinfo"], "timeout": 10})
        ledinfo = resp['stdout'].split('UIDLed')[-1]
        if "BLINKING" not in ledinfo:
            self.assertIsNotNone(None, "Step4: 设置LED 100s失败")
        self.log_step("Step6: 继续等待12s，查看LED 信息")
        time.sleep(12)
        resp = self.bmc_host.run({"command": ["ipmcget -d ledinfo"], "timeout": 10})
        ledinfo = resp['stdout'].split('UIDLed')[-1]
        if "OFF" not in ledinfo:
            self.assertIsNotNone(None, "Step4: 设置LED 100s失败")

    # 测试结束后恢复环境
    def post_test_case(self):
        pass